# Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

project(cmsis_5 LANGUAGES C CXX ASM)

include(FetchContent)

if("cmsis-5" IN_LIST IOTSDK_FETCH_LIST)
    FetchContent_MakeAvailable(cmsis-5)
endif()

if(cmsis-5_POPULATED)
    # User configuration:
    # An application can add the parent directory of its CMSIS configuration
    # header (RTE_Components.h) to this target's include directories.
    add_library(cmsis-config INTERFACE)

    target_link_libraries(cmsis-config
        INTERFACE
            iotsdk-cmsis-core-device
    )

    # NOTE: CMSIS-Core is not defined here. Both, the CMSIS-Core API, and the
    # relevant platform-specific implementation, are expected to be delivered by
    # the user of the SDK and linked to iotsdk-cmsis-core-device CMake target.
    target_link_libraries(iotsdk-cmsis-core-device
        INTERFACE
            cmsis-config
    )

    # CMSIS-RTOS v2 API

    add_library(cmsis-rtos-api
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/Source/os_systick.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/Source/os_tick_ptim.c
        # ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/Source/os_tick_gtim.c # Cortex-A only
    )
    target_include_directories(cmsis-rtos-api
        PUBLIC
            ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/Include
    )
    target_link_libraries(cmsis-rtos-api
        PUBLIC
            iotsdk-cmsis-core-device
    )

    # CMSIS-RTX5

    add_library(cmsis-rtx
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Config/RTX_Config.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Library/cmsis_os1.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_delay.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_evflags.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_evr.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_kernel.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_lib.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_memory.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_mempool.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_msgqueue.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_mutex.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_semaphore.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_system.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_thread.c
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/rtx_timer.c
        # Note: Arm Compiler 6 prefers the GNU assembly format, thus "GCC"
        ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source/GCC/irq_armv8mml.S
    )
    target_include_directories(cmsis-rtx
        PUBLIC
            ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Config
            ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Include
            ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Include1
            ${cmsis-5_SOURCE_DIR}/CMSIS/RTOS2/RTX/Source
    )

    target_compile_definitions(cmsis-rtx
        PRIVATE
            # allows RTX_Config.h to get RTX overrides from RTE_Components.h
            _RTE_
    )

    target_link_libraries(cmsis-rtx
        PUBLIC
            cmsis-rtos-api
    )
endif() # cmsis-5_POPULATED

if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING)
    add_subdirectory(mocks)
endif()
